﻿(function () {
    var googleStockList = {
        pingTarget: "",
        period: "",
        pageNum: ""
    };  //頁面參數 初始化

    function pingURL() {
        var startYear = $('select#ddlStartYear option:selected').text();
        var startMonth = $('select#ddlStartMonth option:selected').val();
        var startDay = $('select#ddlStartDay option:selected').text();
        var endYear = $('select#ddlEndYear option:selected').text();
        var endMonth = $('select#ddlEndMonth option:selected').val();
        var endDay = $('select#ddlEndDay option:selected').text();

        var urlTemplate = new StringBuilder(); //樣板
        urlTemplate.append("{0}?stockCode={1}&period={2}&startYear={3}&startMonth={4}&startDay={5}&endYear={6}&endMonth={7}&endDay={8}&pageNum={9}&dataStartIndex={10}");

        $("#gvList tr").each(function () {
            if (!this.rowIndex) return; // skip first row
            var code = $(this).find("td").eq(2).find("span:first").html()

            for (i = 0; i <= 2000; i = i + gGoogleStockList.pageNum) {
                var url = $.validator.format(urlTemplate.toString()
                    , gGoogleStockList.pingTarget, code, gGoogleStockList.period
                    , startYear, startMonth, startDay
                    , endYear, endMonth, endDay
                    , gGoogleStockList.pageNum, i
                );

                window.open(url);
            }
        });
    } //pingURL 

    function pingURLByCode(code) {
        var startYear = $('select#ddlStartYear option:selected').text();
        var startMonth = $('select#ddlStartMonth option:selected').val();
        var startDay = $('select#ddlStartDay option:selected').text();
        var endYear = $('select#ddlEndYear option:selected').text();
        var endMonth = $('select#ddlEndMonth option:selected').val();
        var endDay = $('select#ddlEndDay option:selected').text();

        var urlTemplate = new StringBuilder(); //樣板
        urlTemplate.append("{0}?stockCode={1}&period={2}&startYear={3}&startMonth={4}&startDay={5}&endYear={6}&endMonth={7}&endDay={8}&pageNum={9}&dataStartIndex={10}");

        for (i = 0; i <= 2000; i = i + gGoogleStockList.pageNum) {
            var url = $.validator.format(urlTemplate.toString()
                    , gGoogleStockList.pingTarget, code, gGoogleStockList.period
                    , startYear, startMonth, startDay
                    , endYear, endMonth, endDay
                    , gGoogleStockList.pageNum, i
                );
            alert(url);
            window.open(url);
        }
    } //pingURLByCode 

    window.pingURL = pingURL;
    window.pingURLByCode = pingURLByCode;
    window.gGoogleStockList = googleStockList;

})(window);

